var tuiwa = require("../../tuiwa.js");
const app = getApp();
Page({
    data: {
        top_banner: [{
            image: "http://fcdn.qiushilaile.com/game/default-42571630636398563.jpg",
            id: 1,
        }, ],
        tools: [{
            name: '身体质量指数BMI',
            image: 'http://fcdn.qiushilaile.com/game/default-10121630635799293.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-54861630635965891.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-2621630635985498.png',
            resultImg: '',
            value: '1',
            person: '123',
        }, {
            name: '中心性肥胖标准',
            image: 'http://fcdn.qiushilaile.com/game/default-53611630636085250.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-12351630636097854.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-53651630636105582.png',
            resultImg: '',
            value: '2',
            person: '242',
        }, {
            name: '身体脂肪率',
            image: 'http://fcdn.qiushilaile.com/game/default-25431630636121182.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-3741630636128869.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-13211630636141474.png',
            resultImg: '',
            value: '3',
            person: '66',
        }, {
            name: '每日所需卡路里',
            image: 'http://fcdn.qiushilaile.com/game/default-40711630636156746.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-16581630636165497.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-73891630636172100.png',
            resultImg: '',
            value: '4',
            person: '56',
        }, {
            name: '健康体重范围',
            image: 'http://fcdn.qiushilaile.com/game/default-21981630636185582.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-57741630636194597.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-55311630636201332.png',
            resultImg: 'http://fcdn.qiushilaile.com/game/default-43791630657334255.jpg',
            value: '5',
            person: '123',
        }, {
            name: '运动健身补水',
            image: 'http://fcdn.qiushilaile.com/game/default-61671630636214269.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-76661630636222595.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-70831630636229698.png',
            resultImg: '',
            value: '6',
            person: '123',
        }, {
            name: '跑步卡路里消耗',
            image: 'http://fcdn.qiushilaile.com/game/default-24311630636248876.jpg',
            bg: 'http://fcdn.qiushilaile.com/game/default-85541630636258270.jpg',
            btn: 'http://fcdn.qiushilaile.com/game/default-26551630636265646.png',
            resultImg: '',
            value: '7',
            person: '123',
        }, ],
    },
    onLoad(query) {
        // this.getUserInfo(); // 获取昵称和头像
        this.getTimer(); // 获取当天日期时间
        let userInfo = tt.getStorageSync('userInfo');
        if (userInfo) {
            this.getUserInfo();
            this.setData({
                isLogin: true
            })
        } else {
            this.setData({
                isLogin: false
            })
        }


        if (query.from === "sharebuttonabc") {
            console.log('sharebuttonabc');
            // do something...
            // 1、日志采集：可以完成通过分享而来的来源数据的统计
        }

    },
    onShow() {
        var interval = setInterval(() => {
            let userInfo = tt.getStorageSync('userInfo');
            console.log(userInfo);
            if (userInfo) {
                this.getUserInfo();
                this.setData({
                    isLogin: true
                })
                clearInterval(interval);
            }
        }, 100);
    },
    closeClick() {
        this.setData({
            layerPopup: false,
        })
    },
    toLogin() {
        this.setData({
            layerPopup: false,
        })
        tuiwa.bindDyWx(); // 请求获取用户信息
        this.getUserInfo(); // 获取昵称和头像
    },
    login() {
        this.setData({
            layerPopup: true,
        })
    },
    onShareAppMessage(option) { // 点击分享
        return {
            title: "这是要转发的小程序标题",
            desc: "这是默认的转发文案，用户可以直接发送，也可以在发布器内修改",
            path: "/pages/home/home", // ?后面的参数会在转发页面打开时传入onLoad方法
            templateId: "",
            success() {
                console.log("转发发布器已调起，并不意味着用户转发成功，微头条不提供这个时机的回调");
            },
            fail() {
                console.log("转发发布器调起失败");
            },
        };
    },
    toMailBox(e) { // 前往健康小科普
        console.log(e.currentTarget.dataset.id);
        if (e.currentTarget.dataset.id == '1') {
            tt.navigateTo({
                url: '/pages/kedu/kedu'
            });
        }
    },
    feedback() { // 反馈
        tt.navigateTo({
            url: '/pages/feedback/feedback'
        });
    },
    test(e) { // 前往工具间（个性化样式）
        console.log(e.currentTarget.dataset.value);
        let value = e.currentTarget.dataset.value;
        if (value == '1') {
            tt.navigateTo({
                url: '/pages/tools/bmi/bmi' // 身体质量指数BMI
            });
        } else if (value == '2') {
            tt.navigateTo({
                url: '/pages/tools/cos/cos' // 中心性肥胖标准
            });
        } else if (value == '3') {
            tt.navigateTo({
                url: '/pages/tools/bfr/bfr' // 身体脂肪率
            });
        } else if (value == '4') {
            tt.navigateTo({
                url: '/pages/tools/dcr/dcr' // 每日所需卡路里
            });
        } else if (value == '5') {
            tt.navigateTo({
                url: '/pages/tools/hwr/hwr' // 健康体重范围
            });
        } else if (value == '6') {
            tt.navigateTo({
                url: '/pages/tools/sfh/sfh' // 运动健身补水
            });
        } else if (value == '7') {
            tt.navigateTo({
                url: '/pages/tools/rcb/rcb' // 跑步卡路里消耗
            });
        }

    },
    toTest(e) { // 前往工具间（通用样式）
        let {
            value,
            person
        } = e.currentTarget.dataset;
        tt.navigateTo({
            url: '/pages/tool/tool?value=' + value + '&person=' + person
        });
    },
    clickTest(e) {
        console.log(e.currentTarget.dataset);
        let {
            name,
            value,
            bg,
            resultImg,
            btn,
        } = e.currentTarget.dataset
        tt.navigateTo({
            url: '/pages/testing/testing?title=' + name +
                '&value=' + value +
                '&bg=' + bg +
                '&resultImg=' + resultImg +
                '&btn=' + btn
        });
    },
    getUserInfo() { // 获取昵称和头像
        tt.getUserInfo({
            withCredentials: true,
            success: res => {
                this.setData({
                    avatarUrl: res.userInfo.avatarUrl,
                    nickName: res.userInfo.nickName
                })
            },
            fail: err => {
                console.log(`getUserInfo 调用失败`, err);
                // that.getUserInfo()
            },
        });
    },
    getTimer() { // 获取时间
        let date = new Date();
        // 获取日期：年月日
        // const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        let monthDate = month + "月";
        // 获取周几
        const weeks = new Array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
        let nowWeek = weeks[new Date().getDay()];
        this.setData({
            monthDate,
            day,
            nowWeek
        });
    },
    showMsg(n) { // 轻提示方法
        tt.showToast({
            title: n,
            icon: "none"
        });
    },
    foodCalorie() { //前往食物卡路里
        tt.navigateTo({
            url: '/pages/foodCalorie/foodCalorie'
        });
    },
    exerciseCalories() { //前往食运动耗卡
        tt.navigateTo({
            url: '/pages/exerciseCalories/exerciseCalories'
        });
    },
    healthFood() { //前往健康食谱
        tt.navigateTo({
            url: '/pages/healthFood/healthFood'
        });
    },
    healthSciences() {
        tt.navigateTo({
            url: '/pages/mailBox/mailBox'
        });
    },
})